home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / INHERIT1.CPP < prev    next >
Text File  |  1991-04-28  |  3KB  |  122 lines

  1.                                   // Chapter 8 - Program 1
  2. #include "iostream.h"
  3.  
  4. class vehicle {
  5.    int wheels;
  6.    float weight;
  7. public:
  8.    void initialize(int in_wheels, float in_weight);
  9.    int get_wheels(void) {return wheels;}
  10.    float get_weight(void) {return weight;}
  11.    float wheel_loading(void) {return weight/wheels;}
  12. };
  13.  
  14.  
  15.  
  16.  
  17. class car : public vehicle {
  18.    int passenger_load;
  19. public:
  20.    void initialize(int in_wheels, float in_weight, int people = 4);
  21.    int passengers(void) {return passenger_load;}
  22. };
  23.  
  24.  
  25.  
  26.  
  27. class truck : public vehicle {
  28.    int passenger_load;
  29.    float payload;
  30. public:
  31.    void init_truck(int how_many = 2, float max_load = 24000.0);
  32.    float efficiency(void);
  33.    int passengers(void) {return passenger_load;}
  34. };
  35.  
  36.  
  37.  
  38.  
  39. main()
  40. {
  41. vehicle unicycle;
  42.  
  43.    unicycle.initialize(1, 12.5);
  44.    cout << "The unicycle has " << 
  45.                                 unicycle.get_wheels() << " wheel.\n";
  46.    cout << "The unicycle's wheel loading is " << 
  47.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  48.    cout << "The unicycle weighs " << 
  49.                              unicycle.get_weight() << " pounds.\n\n";
  50.  
  51. car sedan;
  52.  
  53.    sedan.initialize(4, 3500.0, 5);
  54.    cout << "The sedan carries " << sedan.passengers() << 
  55.                                                     " passengers.\n";
  56.    cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  57.    cout << "The sedan's wheel loading is " << 
  58.                     sedan.wheel_loading() << " pounds per tire.\n\n";
  59.  
  60. truck semi;
  61.  
  62.    semi.initialize(18, 12500.0);
  63.    semi.init_truck(1, 33675.0);
  64.    cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  65.    cout << "The semi's efficiency is " << 
  66.                           100.0 * semi.efficiency() << " percent.\n";
  67. }
  68.  
  69.  
  70.  
  71.  
  72.               // initialize to any data desired
  73. void
  74. vehicle::initialize(int in_wheels, float in_weight)
  75. {
  76.    wheels = in_wheels;
  77.    weight = in_weight;
  78. }
  79.  
  80.  
  81.  
  82. void
  83. car::initialize(int in_wheels, float in_weight, int people)
  84. {
  85.    passenger_load = people;
  86.    vehicle::initialize(in_wheels, in_weight);
  87. }
  88.  
  89.  
  90.  
  91.  
  92. void
  93. truck::init_truck(int how_many, float max_load)
  94. {
  95.    passenger_load = how_many;
  96.    payload = max_load;
  97. }
  98.  
  99.  
  100.  
  101. float
  102. truck::efficiency(void)
  103. {
  104.    return payload / (payload + get_weight());
  105. }
  106.  
  107.  
  108.  
  109.  
  110. // Result of execution
  111. //
  112. // The unicycle has 1 wheel.
  113. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  114. // The unicycle weighs 12.5 pounds.
  115. //
  116. // The sedan carries 5 passengers.
  117. // The sedan weighs 3500 pounds.
  118. // The sedan's wheel loading is 875 pounds per tire.
  119. //
  120. // The semi weighs 12500 pounds.
  121. // The semi's efficiency is 72.929072 percent.
  122.